package com.study.talk.ui.adapter.chat

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.talk.R
import com.study.talk.ui.adapter.base.BaseListAdapter
import com.study.talk.ui.app.base.BaseActivity
import com.study.talk.databinding.CachesItemBinding
import com.study.talk.data.model.ChatCache
import com.study.talk.utils.alert.AlertUtil
import com.study.talk.utils.extras.toChat
import com.study.talk.utils.extras.toGroupChat

class ChatCachesAdapter(val activity: BaseActivity) :
    BaseListAdapter<ChatCache, ChatCachesAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.caches_item, parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val cache = getItem(position)
        holder.binding.cache = cache
        holder.binding.root.setOnClickListener {
            activity.apply {
                when (cache.chatType) {
                    1 -> toChat(cache.userId)
                    2 -> toGroupChat(cache.groupId)
                }
            }
        }
        holder.binding.root.setOnLongClickListener {
            AlertUtil.showCustomDialog(activity, "删除会话提醒", "确顶要删除该对话吗？") { b ->
                if (b) activity.app.cacheDao().remove(cache)
            }
            true
        }
    }

    class ViewHolder(val binding: CachesItemBinding) : RecyclerView.ViewHolder(binding.root)

}